package com.example.recyclerview2;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

//    private String[] data = {"ban", "ban", "ban", "ban", "ban", "ban",
//            "ban", "ban", "ban", "ban", "ban", "ban", "ban", "ban"};

    private List<Fruit> fruitList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFruits();  //初始化水果数据

        RecyclerView recyclerView = findViewById(R.id.rv_1);

        //瀑布布局
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);


        //        LinearLayoutManager layoutManager = new LinearLayoutManager(this);

        //设置布局排列
//        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);


        recyclerView.setLayoutManager(layoutManager);
        FruitAdpater adpater = new FruitAdpater(fruitList);
        recyclerView.setAdapter(adpater);


    }

//    private void initFruits(){
//        for(int i =0;i <20;i++){
//            Fruit ban = new Fruit("ban", R.drawable.ic_launcher_foreground);
//            fruitList.add(ban);
//        }
//    }

    private void initFruits() {
        for (int i = 0; i < 20; i++) {
            Fruit ban = new Fruit(getRandomLengthName("ban"), R.drawable.ic_launcher_background);
            fruitList.add(ban);
        }
    }


    private String getRandomLengthName(String name) {
        Random random = new Random();
        int length = random.nextInt(20) + 1;
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            builder.append(name);
        }
        return builder.toString();

    }
}